// pages/attractionsDetails/attractionsDetails.js

//获取应用实例
const app = getApp()
// 引用百度地图微信小程序JSAPI模块
const BMap = require('../../libs/bmap-wx.min.js');
var bmap;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    testText: '',
    checkSite: '',
    address: '',
    longitude: '', //经度
    latitude: '', //纬度
    mylatitude: '', //我的经度
    mylongitude: '', //我的纬度
    commentOn: '2',
    distance: '0', //距离
    card_status: 'false',
    markers: [{ //标记点用于在地图上显示标记的位置
      id: 1,
      latitude: '',
      longitude: '',
      iconPath: '../public/image/location.png',
      width: 1,
      height: 1,
    }],
    list: "",
    scenicId: '',
    userId: '',
    scenicName: '',
    INDEX: '',
  },
  goPageEdit: function () {
    var that = this
    wx.navigateTo({
      url: '/pages/edit/edit?userId=' + this.data.userId + '&scenicId=' + this.data.scenicId
    })
  },
  showImg: function (e) {
    var src = e.currentTarget.dataset.src
    var imgUrl = e.currentTarget.dataset.imgurls
    wx.previewImage({
      urls: imgUrl,
      current: src,
    })

  },

  likeup: function (e) {
    var that = this
    var scenicId = that.data.scenicId
    var clockinid = e.currentTarget.dataset.id
    console.log(clockinid)
    wx.request({
      url: 'http://120.25.151.125:8080/travel/scenic/likeUpClockIn',
      data: {
        clockInId: clockinid,
        scenicId: scenicId
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        for (let i = 0; i < res.data.length; i++) {
          if (clockinid == res.data[i].clockId) {
            console.log(res.data[i].clockId)
            that.setData({
              INDEX: i
            })
            console.log(i)
          }
        }
        console.log(res.data[that.data.INDEX].likeCount)
        that.setData({
          ["list[" + that.data.INDEX + "].likeCount"]: res.data[that.data.INDEX].likeCount
        })
        console.log(that.data.list)
      }

    })
    // wx.navigateTo({
    //   url: '/pages/attractionsDetails/attractionsDetails?scenicId='+scenicId
    //   })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    const that = this;

    // 实例化百度地图API核心类
    bmap = new BMap.BMapWX({
        ak: app.globalData.ak
      }),

      setInterval(function () {
        var fail = function (data) { //请求失败
          var searchResult = [];
          that.setData({
            searchResult: searchResult
          })
        };
        var success = function (data) { //请求成功
          var searchResult = [];
          for (var i = 0; i < 1; i++) { //搜索列表只显示10条
            if (data.result[i].location) {
              searchResult.push(data.result[i]);
              // console.log(data.result[i].location)
            }
            that.setData({
              searchResult: searchResult,
              latitude: data.result[i].location.lat,
              longitude: data.result[i].location.lng,
            });
          }
        }

        // 发起suggestion检索请求 --模糊查询
        bmap.suggestion({
          query: that.data.checkSite,
          city_limit: false,
          fail: fail,
          success: success
        });


        // 循环执行代码  
        app.getLocation(function (location) {
          // console.log("当前位置："+location.latitude+","+location.longitude);
          that.setData({
            mylongitude: location.longitude,
            mylatitude: location.latitude,
          });
        });

        // console.log("结果：",that.data.searchResult)
        //计算经纬度距离函数
        var la1 = that.data.latitude
        var lo1 = that.data.longitude
        var la2 = that.data.mylatitude
        var lo2 = that.data.mylongitude
        // console.log("当前位置",la1,lo1)
        // console.log("目标位置",la2,lo2)
        var La1 = la1 * Math.PI / 180.0;
        var La2 = la2 * Math.PI / 180.0;
        var La3 = La1 - La2;
        var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
        s = s * 6378.137;
        s = Math.round(s * 10000) / 10000;
        s = s.toFixed(2);
        // console.log(s)
        that.setData({
          distance: s
        })


      }, 2000) //循环时间 这里是1秒   

    var scenicId = options.scenicId
    var imgList = []
    imgList = options.imglist.split(",")
    wx.request({
      url: 'http://120.25.151.125:8080/travel/scenic/getClockIn',
      data: {
        scenicId: scenicId
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        //将获取到的图片地址由字符串形式转变为数组形式
        let data = res.data;
        for (let item of data) {
          let _imgUrl = item.imgUrl;
          item.imgUrl = _imgUrl.split(",");
        }
        //将获取到的json数据，存在名字叫list的这个数组中
        console.log(res.data)
        console.log(options.imglist)
        that.setData({
          address: options.address,
          testText: imgList,
          userId: res.data[0].userId,
          scenicId: scenicId,
          list: res.data,
          scenicAdd: res.data[0].scenicAdd,
          checkSite: res.data[0].scenicName,
          //res代表success函数的事件对，data是固定的，list是数组
        })
        console.log(that.data.testText)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var address = this.data.address
    wx.reLaunch({
      url: '../attractionsList/attractionsList?address=' + address,
    })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})